package moodish.test;

import java.util.Observer;
import java.util.Random;

import moodish.comm.ServerComm;
import moodish.comm.ServerSideMessage.Type;
import moodish.dummy.comm.SSM;
import moodish.dummy.comm.server.ServerCommDummy;

import org.junit.Assert;
import org.junit.Test;


public class ServerCommDummyTestCase {

	public static final String UNKNOWN = "unknown nickname";
	public static final String ERRROR = "ERROR";
	private Random random = new Random();

	private int generateRandomPort() {
		return random.nextInt(ServerCommDummy.MAX_PORT_NUMBER) + ServerCommDummy.MIN_PORT_NUMBER;
	}

	@Test
	public void polimorfismServerCommDummyTest1() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);
		Assert.assertTrue(server instanceof ServerCommDummy);
	}

	@Test
	public void polimorfismServerCommDummyTest2() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);
		Assert.assertTrue(server instanceof ServerComm);
	}

	@Test
	public void polimorfismServerCommDummyTest3() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);
		Assert.assertTrue(server instanceof Runnable);
	}

	@Test
	public void polimorfismServerCommDummyTest4() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);
		Assert.assertTrue(server instanceof Observer);
	}

	@Test
	public void getNextMessageTest() {
		int randomPort = generateRandomPort();
		final ServerCommDummy server = new ServerCommDummy(randomPort);
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				server.getNextMessage();
			}
		});

		thread.start();
		thread.interrupt();
	}

	@Test
	public void disconnectClientTest() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);

		Assert.assertFalse(server.clientIsConnected(UNKNOWN));
		server.disconnectClient(UNKNOWN);
	}

	
	
	@Test
	public void unknownNicknameTest() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);

		Assert.assertFalse(server.clientIsConnected(UNKNOWN));
		server.sendError(UNKNOWN, ERRROR);
	}
	
	
	@Test
	public void acknowlegdeToUnknownNicknameTest() {
		int randomPort = generateRandomPort();
		ServerCommDummy server = new ServerCommDummy(randomPort);

		Assert.assertFalse(server.clientIsConnected(UNKNOWN));
		server.sendAcknowledge(UNKNOWN);
	}

	@Test
	public void addMessaheToQueue() {
		int randomPort = generateRandomPort();
		final ServerCommDummy server = new ServerCommDummy(randomPort);

		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				Thread.currentThread().interrupt();
				server.update(null, new SSM(Type.MOODISH_MESSAGE, UNKNOWN, null));
			}
		});
		thread.start();
	}
}
